const express = require('express');
const multer = require('multer');
const svgCaptcha = require('svg-captcha');
const router = express.Router();
const upload = multer({
	storage: multer.diskStorage({
		destination: function(req, file, cb) {
			cb(null, './uploads')
		},
		filename: function(req, file, cb) {
			let str = file.originalname.split('.');
			cb(null, Date.now() + '.' + str[1]);
		}
	})
});
router.post('/upload', upload.array("file", 20), function(req, res, next) {
	res.json({
		code: 200,
		data: '/uploads/' + req.files[0].filename
	})
});
router.get('/captcha', function(req, res, next) {
	let captcha = svgCaptcha.create({
		inverse: false, // 翻转颜色 
		fontSize: 48, // 字体大小 
		noise: 4, // 噪声线条数 
		size: 4, // 验证码长度
		ignoreChars: '0o1i', // 验证码字符中排除 0o1i
	});
	// 保存到session,忽略大小写 
	req.session.captcha = captcha.text.toLowerCase();
	res.cookie('captcha', req.session.captcha);
	res.type('svg');
	res.send(String(captcha.data));
});
module.exports = router;